from django.shortcuts import render
from BASE.interface import get_backend_data as backend


# Create your views here.
class Comic:
    __comic_html = "Comic/comic.html"
    __cover_html = "Comic/cover.html"
    __readingRoom_html = "Comic/readingRoom.html"

    def comic_view(self, request):
        comics = self.__get_comic_data(form_data={
            "function": "Showall"
        })
        read = self.__get_comic_data(form_data={
            "function": "Showrank",
            "rank": "read",
        })
        update = self.__get_comic_data(form_data={
            "function": "Showupdate",
        })
        collection = self.__get_comic_data(form_data={
            "function": "Showrank",
            "rank": "collection",
        })
        return render(request, self.__comic_html, context={
            "comics": comics,
            "read": read,
            "update": update,
            "collection": collection
        })
        pass

    def cover_view(self, request, cId):
        session = None
        try:
            session = request.COOKIES['session']
        except KeyError:
            session = ''
        comic = self.__get_comic_data(
            url="http://193.112.72.237/Web-comfun/web/index.php?r=commic/index",
            form_data={
                "function": "Showcom",
                "session": session,
                "cId": cId
            }
        )
        return render(request, self.__cover_html, {"comic": comic})
        pass

    def readingRoom_view(self, request, cId, episode):
        """
        :param request:
        :param cId:  漫画ID
        :param episode: 漫画话数,
        :return:
        """
        session = None
        try:
            session = request.COOKIES['session']
        except KeyError:
            session = ''
        # -------------------获取漫画链接---------------------------------------
        comic = self.__get_comic_data(
            url="http://193.112.72.237/Web-comfun/web/index.php?r=commic/index",
            form_data={
                "function": "Showcom",
                "session": session,
                "cId": cId
            }
        )
        # --------------------更新阅读历史-----------------------------------
        reading_status = self.__get_comic_data(form_data={
            "function": "Reading",
            "cId": cId,
            "reading": "{}/{}".format(episode, 1),
            "session": session
        })
        episode = str(episode)
        saveurl = comic['episodes_info'][episode]["saveurl"]
        pages = int(comic['episodes_info'][episode]["pages"])
        return render(request, self.__readingRoom_html, {
            "saveurl": saveurl,
            'pages': range(1, pages + 1),
            "comic": comic,
            'episode': episode
        })
        pass

    def __get_comic_data(self, url="http://193.112.72.237/Web-comfun/web/index.php?r=commic/index", form_data=None):
        return backend.get_comic_data(url, form_data)

        pass

    pass


comics_views = Comic()
